﻿// Autore: Mattia Barrasso

using System.Linq;
using NUnit.Framework;
using RecipesManager.Data;
using TechTalk.SpecFlow;
using White.Core.UIItems;
using White.Core.UIItems.WindowItems;

namespace RecipesManager.Specs.Categoria
{
    [Binding, Scope(Feature = "RimuoviCategoria")]
    public class RimuoviCategoria_Steps : CategorieBDD
    {
        Window _dialog;
        Button _confirm;
        Button _cancel;
        Button _accept;
        Button _deleteCategory;

        void getConfirmDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _confirm = MainWindow.Get<Button>("6");
            _cancel = MainWindow.Get<Button>("7");
        }

        void getAcceptDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _accept = MainWindow.Get<Button>("2");
        }

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [Given(Regex = @"la categoria ([a-zA-Z]+) esiste")]
        public void DatoCategoriaEsistente(string categoria)
        {
            Manager.Categories.Add(categoria);
            Assert.That(Manager.Categories.Contains(categoria));
        }

        [Given(Regex = @"l'ingrediente ([a-zA-Z]+) esiste")]
        public void DatiIngredienteEsistente(string ingrediente)
        {
            Manager.Ingredients.Add(ingrediente, MeasureType.Weight);
            Assert.That(Manager.Ingredients.Contains(ingrediente));
        }

        [Given(Regex = @"la categoria ([a-zA-Z]+) contiene l'ingrediente ([a-zA-Z]+)")]
        public void DatoIngredienteAppartente(string categoria, string ingrediente)
        {
            Assert.That(Manager.Ingredients.Contains(ingrediente));
            Assert.That(Manager.Categories.Contains(categoria));
            Manager.Categories[categoria].Ingredients.Add(ingrediente);
        }

        [Given(Regex = "l'ospite \"([a-zA-Z ]+)\" esiste")]
        public void DatoOspiteEsistente(string ospite)
        {
            Manager.Guests.Add(ospite);
        }

        [Given(Regex = "l'ospite \"([a-zA-Z ]+)\" è intollerante alla categoria \"([a-zA-Z ]+)\"")]
        public void DatoOspiteIntollerante(string ospite, string categoria)
        {
            Assert.That(Manager.Categories.Contains(categoria));
            Assert.That(Manager.Guests.Contains(ospite));
            Manager.Guests[ospite].CategoryIntolerances.Add(categoria);
        }

        [Given(Regex = "l'ospite \"([a-zA-Z ]+)\" preferisce la categoria \"([a-zA-Z ]+)\"")]
        public void DatoPrefenzaOspite(string ospite, string categoria)
        {
            Assert.That(Manager.Categories.Contains(categoria));
            Assert.That(Manager.Guests.Contains(ospite));
            Manager.Guests[ospite].CategoryRatings.Add(categoria, FoodRating.VeryGood);
        }

        [Given(Regex = @"seleziono la categoria ([a-zA-Z]+)")]
        public void DatoModificoIngrediente(string categoria)
        {
            NavigateToCategories();
            GetCategoriesUi();
            _deleteCategory = MainWindow.Get<Button>("bt_delete");

            Assert.That(Manager.Categories.Contains(categoria));
            Assert.That(_categories.Items.Any(item => item.Text.Equals(categoria)));
            _categories.Select(categoria);
        }

        [When(Regex = @"elimino la categoria")]
        public void QuandoElimino()
        {
            _deleteCategory.Click();
        }

        [Then(Regex = @"appare un messaggio")]
        public void AlloraSuccesso()
        {
            Assert.That(MainWindow.ModalWindows().Count() == 1);
        }

        [When(Regex = "confermo")]
        public void QuandoConfermo()
        {
            this.getConfirmDialogUi();
            _confirm.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "annullo")]
        public void QuandoAnnullo()
        {
            this.getConfirmDialogUi();
            _cancel.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [When(Regex = "accetto")]
        public void QuandoAccetto()
        {
            this.getAcceptDialogUi();
            _accept.Click();
            Assert.That(!MainWindow.ModalWindows().Any());
        }

        [Then(Regex = "la categoria ([a-zA-Z]+) non esiste")]
        public void AlloraIngredienteNonEsiste(string categoria)
        {
            Assert.That(!_categories.Items.Exists(item => item.Text.Equals(categoria)));
            _backToMainMenu.Click();
            Assert.That(!Manager.Categories.Contains(categoria));
        }

        [Then(Regex = "la categoria ([a-zA-Z]+) esiste")]
        public void AlloraIngredienteEsiste(string categoria)
        {
            Assert.That(_categories.Items.Exists(item => item.Text.Equals(categoria)));
            _backToMainMenu.Click();
            Assert.That(Manager.Categories.Contains(categoria));
        }
    }
}
